/**
 * 路径导入辅助模块 - 统一配置入口模式
 * 通过统一配置入口引用PLM项目的辅助模块，避免直接依赖路径
 */

// 导入路径解析器
const pathResolver = require('../../../config/path_resolver.js').getRootConfigPath('path_resolver.js').getRootConfigPath('path_resolver.js');

// 尝试从项目级别的统一配置入口获取模块
try {
  // 加载项目级别的path_import
  const projectPathImport = require('../../../config/path_resolver.js').getRootConfigPath('path_resolver.js').getRootConfigPath('path_import.js');
  const helpers = projectPathImport.getGlobalHelpers('page');
  
  // 解构并导出所需的辅助模块
  module.exports = {
    pathResolver,
    pageHelper: helpers.pageHelper,
    cloudHelper: helpers.cloudHelper,
    routerHelper: helpers.routerHelper,
    validate: helpers.validate,
    // NPM相关
    createStoreBindings: helpers.createStoreBindings,
    mobx: helpers.mobx,
    dayjs: helpers.dayjs
  };
} catch (e) {
  console.error('加载PLM项目统一配置入口失败，尝试加载全局配置', e);
  
  try {
    // 从全局统一配置入口导入辅助模块
    const { Helpers, NpmPackages } = require('../../../config/path_resolver.js').getRootConfigPath('path_resolver.js').getRootConfigPath('module_config.js');
    
    // 导出所有模块
    module.exports = {
      pathResolver,
      pageHelper: Helpers.pageHelper,
      cloudHelper: Helpers.cloudHelper,
      requestHelper: Helpers.requestHelper,
      timerHelper: Helpers.timerHelper,
      cacheHelper: Helpers.cacheHelper,
      routerHelper: Helpers.routerHelper,
      timeHelper: Helpers.timeHelper,
      dataHelper: Helpers.dataHelper,
      formHelper: Helpers.formHelper,
      validate: Helpers.validate,
      // NPM相关
      createStoreBindings: NpmPackages.createStoreBindings,
      mobx: NpmPackages.mobx,
      dayjs: NpmPackages.dayjs
    };
  } catch (err) {
    console.error('加载全局配置入口失败，提供备用实现', err);
    
    // 提供备用实现
    const pageHelper = require('../../../config/project_helpers_config').pageHelper;
    const cloudHelper = require('../../../config/project_helpers_config').cloudHelper;
    const validate = require('../../../config/path_resolver.js').pageHelper;
    
    // 备用的routerHelper实现
    const routerHelper = {
      navigateTo: function(url, params) {
        console.log('使用备用的routerHelper.navigateTo');
        
        // 处理参数
        if (params && Object.keys(params).length > 0) {
          const query = Object.keys(params)
            .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`)
            .join('&');
          url = `${url}${url.includes('?') ? '&' : '?'}${query}`;
        }
        
        // 调用wx原生方法
        wx.navigateTo({ url });
      }
    };
    
    // 导出备用实现
    module.exports = {
      pathResolver,
      pageHelper,
      cloudHelper,
      routerHelper,
      validate,
      // 备用NPM实现
      createStoreBindings: function(page, options) {
        console.warn('使用了备用的createStoreBindings实现');
        const { store, fields, actions } = options;
        
        // 绑定字段
        if (fields && Array.isArray(fields)) {
          fields.forEach(field => {
            if (store[field] !== undefined) {
              page.setData({ 
                [field]: typeof store[field] === 'function' ? store[field]() : store[field] 
              });
            }
          });
        }
        
        // 绑定方法
        if (actions && Array.isArray(actions)) {
          actions.forEach(action => {
            if (typeof store[action] === 'function') {
              page[action] = function(...args) {
                return store[action](...args);
              };
            }
          });
        }
        
        // 返回绑定对象
        return {
          destroyStoreBindings: function() {
            console.log('销毁MobX备用绑定');
          }
        };
      }
    };
  }
} 